package org.cupk2.zhzm.controller;

import jakarta.validation.Valid;
import org.cupk2.zhzm.dto.ZhzmTagDto;
import org.cupk2.zhzm.domain.ZhzmTag;
import org.cupk2.zhzm.service.ZhzmTagServiceInter;
import org.cupk2.tools.bean.CupkBeanUtils;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataQueryDto;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.db.commons.DBSort;
import org.cupk2.tools.web.CupkResponseCode;
import org.cupk2.tools.web.CupkResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**系统标签 
 * @author cbtg自动生成  2025-2-11 15:32:01 
 */
@RestController
@RequestMapping("/api/zhzmtag")
public class ZhzmTagController {
    @Autowired
    private ZhzmTagServiceInter zhzmTagServiceImpl;

    /**
     * 条件查询
     *
     * @param query
     * @return
     */
    @PreAuthorize("@cupkAction.has('zhzmTag.query')")
    @PostMapping("/query")
    public CupkResponseResult queryAll(@RequestBody DataQueryDto query) {
        DataQuery dataQuery = DataQuery.getInstance(query);
        dataQuery.addDefaultSort(new DBSort("ETIME", DBSort.SORT_TYPE.DESC));
        dataQuery.setRuleAsSql("default", "and (NAME like '%?%')");
        DataResult result = zhzmTagServiceImpl.searchZhzmTag(dataQuery);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, result);
    }
    
    /**[tree：树形结构使用]
     * 条件查询(树形结构)
     *
     * @param query
     * @return 
    @PreAuthorize("@cupkAction.has('zhzmTag.query')")
    @PostMapping("/query")
    public CupkResponseResult queryAll(@RequestBody DataQueryDto query) {
        DataQuery dataQuery = DataQuery.getInstance(query);
        dataQuery.addDefaultSort(new DBSort("SORTCODE", DBSort.SORT_TYPE.ASC));
        dataQuery.setRuleAsSql("default", "and (NAME like '%?%')");
        if(!dataQuery.hasRules()){
            dataQuery.addRule(new DBRule("PARENTID","NONE","="));
        }
        DataResult result = zhzmTagServiceImpl.searchZhzmTag(dataQuery);
        result.runDataHandle(new ResultDataHandle() {
            @Override
            public void handle(Map<String, Object> row) {
                row.put("isLeaf", zhzmTagServiceImpl.getNum(DataQuery.getInstance().addRule(new DBRule("PARENTID", row.get("ID"), "="))) <= 0);
            }
        });
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, result);
    }    
    */
    
    
    /**[tree：树形结构使用]
     * 查询树
     *
     * @param query
     * @return
     
    @PreAuthorize("@cupkAction.has('zhzmTag.query')")
    @PostMapping("/tree")
    public CupkResponseResult treeload(@RequestBody DataQueryDto query) {
        DataQuery dataQuery = DataQuery.getInstance(query);
        dataQuery.addDefaultSort(new DBSort("SORTCODE", DBSort.SORT_TYPE.ASC));
        dataQuery.setPageSizeAll();
        dataQuery.setRuleAsSql("default", "and (NAME like '%?%')");
        Object parentid = dataQuery.getRuleValue("PARENTID");
        if (parentid == null || StringUtils.isBlank(parentid.toString())) {
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> node = new HashMap<>();
            node.put("ID", "NONE");
            node.put("value", "NONE");
            node.put("ICON", Icons.HomeFilled.getSvg());
            node.put("NAME", "系统标签");
            node.put("isLeaf", zhzmTagServiceImpl.getNum(DataQuery.getInstance().addRule(new DBRule("PARENTID", "NONE", "="))) <= 0);
            list.add(node);
            DataResult result = new DataResult(list, 1);
            return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, result);
        }
         DataResult result = zhzmTagServiceImpl.searchZhzmTag(dataQuery);
        result.runDataHandle(new ResultDataHandle() {
            @Override
            public void handle(Map<String, Object> row) {
                row.put("isLeaf", zhzmTagServiceImpl.getNum(DataQuery.getInstance().addRule(new DBRule("PARENTID", row.get("ID"), "="))) <= 0);
                row.put("value", row.get("ID"));
            }
        });
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, result);
    }
    */
    
    
        /**[tree：树形结构使用]
     * 移动节点结构
     *
     * @param paras
     * @return
  
    @PreAuthorize("@cupkAction.has('zhzmTag.move')")
    @PostMapping("/move")
    public CupkResponseResult move(@RequestBody IdAndIdsDto paras) {
        zhzmTagServiceImpl.moveTreeNode(paras.getIds(), paras.getId());
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, null);
    }
       */

      /**[tree：树形结构使用]
     * 自动设置排序
     *
     * @param ids
     * @return
       */
    @PreAuthorize("@cupkAction.has('zhzmTag.autosort')")
    @PostMapping("/autosort")
    public CupkResponseResult autosort(@RequestBody List<String> ids) {
        zhzmTagServiceImpl.autoSort(ids);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, null);
    }



    /**
     * 添加数据
     *
     * @param zhzmTagDto
     * @return
     */
    @PreAuthorize("@cupkAction.has('zhzmTag.add')")
    @PostMapping
    public CupkResponseResult addSubmit(@Valid @RequestBody ZhzmTagDto zhzmTagDto) {
        ZhzmTag zhzmTag = zhzmTagServiceImpl.insertZhzmTagEntity(CupkBeanUtils.copyProperties(zhzmTagDto, new ZhzmTag()));
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, zhzmTag.getId());
    }

    /**修改数据
     * @param id
     * @param zhzmTagDto
     * @return
     */
    @PreAuthorize("@cupkAction.has('zhzmTag.edit')")
    @PutMapping("/{id}")
    public CupkResponseResult editSubmit(@PathVariable String id, @Valid @RequestBody ZhzmTagDto zhzmTagDto) {
        zhzmTagDto.setId(id);
        zhzmTagServiceImpl.editZhzmTagEntity(CupkBeanUtils.copyProperties(zhzmTagDto, new ZhzmTag()));
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**删除数据
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('zhzmTag.del')")
    @DeleteMapping("/{id}")
    public CupkResponseResult delSubmit(@PathVariable String id) {
        zhzmTagServiceImpl.delZhzmTag(id);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, id);
    }

    /**
     * 批量删除数据
     *
     * @param ids 要删除的记录ID列表
     * @return 操作结果
     */
    @PreAuthorize("@cupkAction.has('zhzmTag.del')")
    @DeleteMapping("/batch")
    public CupkResponseResult batchDelSubmit(@RequestBody List<String> ids) {
        for (String id : ids) {
            zhzmTagServiceImpl.delZhzmTag(id);
        }
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**数据浏览
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('zhzmTag.info')")
    @GetMapping("/{id}")
    public CupkResponseResult info(@PathVariable String id) {
        ZhzmTag zhzmTag = zhzmTagServiceImpl.getZhzmTagById(id);
        return new CupkResponseResult(CupkResponseCode.SUCESS, CupkBeanUtils.copyProperties(zhzmTag, new ZhzmTagDto()));
    }
}
